/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package scheduler.view.windows;

import com.sun.javafx.collections.ObservableListWrapper;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.TextField;
import javafx.scene.control.Tooltip;
import javafx.stage.Stage;
import scheduler.core.task.Taskj;
import scheduler.core.utils.TaskjValidationException;
import scheduler.view.dialogs.DialogUtils;

/**
 * FXML Controller class
 *
 * @author Dima
 */
public class TaskCreatorController implements Initializable {
    static private int numOfPriors = 1;
    @FXML
    private TextField identifier;
    @FXML
    private ChoiceBox priority;
    @FXML
    private TextField intencity;
    @FXML
    private TextField averageTime;
    @FXML
    private Button okButton;
    @FXML
    private Button chancelButton;
    private Tooltip errorTooltip;
    
    private Taskj task = null;
    private boolean edit = false;
    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        errorTooltip = new Tooltip();
        identifier.setText(Taskj.getNextId().toString());
        priority.setItems(new ObservableListWrapper(new ArrayList()));
        for(int i = 1; i <= numOfPriors; i++){
            priority.getItems().add(new Integer(i).toString());
        }
        priority.getSelectionModel().selectFirst();
    }    
    
    @FXML
    void onOkAction(ActionEvent event) throws IOException {
        if (identifier.getText().isEmpty()) {
            DialogUtils.showErrorDialog("Порожній ідентифікатор");
            return;
        } else if (intencity.getText().isEmpty()) {
            DialogUtils.showErrorDialog("Порожня інтенсивність");
            return;
        } else if (averageTime.getText().isEmpty()) {
            DialogUtils.showErrorDialog("Порожній середній час");
            return;
        }
        try {
            if (!edit) {
                task = new Taskj(Integer.parseInt(priority.getSelectionModel().getSelectedItem().toString()), Double.parseDouble(intencity.getText()), Double.parseDouble(averageTime.getText()));
            } else {
                task.setId(Integer.parseInt(identifier.getText()));
                task.setPrior(Integer.parseInt(priority.getSelectionModel().getSelectedItem().toString()));
                task.setAj(Double.parseDouble(intencity.getText()));
                task.setBj(Double.parseDouble(averageTime.getText()));
            }
        } catch (NumberFormatException | TaskjValidationException ex) {
            DialogUtils.showErrorDialog(ex.getMessage());
            //Logger.getLogger(TaskCreatorController.class.getName()).log(Level.SEVERE, null, ex);
            if (!edit) {
                task = null;
            }
            return;
        }
        
        ((Stage)okButton.getScene().getWindow()).close();
    }
    
    public void setValues(Taskj task) {
        this.task = task;
        identifier.setText(String.valueOf(task.getId()));
        priority.setItems(new ObservableListWrapper(new ArrayList()));
        for(int i = 1; i <= numOfPriors; i++){
            priority.getItems().add(new Integer(i).toString());
        }
        for(int i = numOfPriors + 1; i <= task.getPrior(); i++){
            priority.getItems().add(new Integer(i).toString());
        }
        int selectedPrior = task.getPrior();
        priority.getSelectionModel().selectFirst();
        for(int i = 1; i < selectedPrior; i++){
            priority.getSelectionModel().selectNext();
        } 
        intencity.setText(String.valueOf(task.getAj()));
        averageTime.setText(String.valueOf(task.getBj()));
        edit = true;
    }
    
    @FXML
    void onChancelAction(ActionEvent event) {
        ((Stage)okButton.getScene().getWindow()).close();
    }
    
    static public void setNumOfPriors(int num){
        numOfPriors = num;
    }

    public Taskj getTask() {
        return task;
    }
}
